﻿using HandyControl.Controls;
using Prism.Commands;
using Prism.Events;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZoneLineNew.Common;
using ZoneLineNew.Core;
using ZoneLineNew.Services.DB;
using ZoneLineNew.Services.Message;
using ZoneLineNew.ViewModelBase;
using ZoneLineNew.Views.Dialogs;

namespace ZoneLineNew.ViewModels {

    internal class SysSettingViewModel : NavigationViewModel {
        public SysSettingViewModel(
            IRegionManager regionManager,
            IDialogService dialog,
            IMessageService messageBox,
            IEventAggregator eventAggregator,
            IDbOperate<IFreeSqlMainContext> mainDbOperate
            ) : base(regionManager, dialog, messageBox, eventAggregator, mainDbOperate) {

        }

        #region Props
        private bool _isAuth;

        public bool IsAuth {
            get { return _isAuth; }
            set { _isAuth = value; base.RaisePropertyChanged(); }
        }

        private string _lockText = "解锁";
        /// <summary>
        /// 锁文本
        /// </summary>
        public string LockText {
            get { return _lockText; }
            set { _lockText = value; base.RaisePropertyChanged(); }
        }

        private bool _isEnabledMaterialVerify;
        /// <summary>
        /// 是否启用物料验证
        /// </summary>
        public bool IsEnabledMaterialVerify {
            get { return _isEnabledMaterialVerify; }
            set { _isEnabledMaterialVerify = value; base.RaisePropertyChanged(); }
        }


        private bool _isEnabledCylinderCodeVerify;
        /// <summary>
        /// 是否启用工体码验证
        /// </summary>
        public bool IsEnabledCylinderCodeVerify {
            get { return _isEnabledCylinderCodeVerify; }
            set { _isEnabledCylinderCodeVerify = value; base.RaisePropertyChanged(); }
        }

        private int _apiRequestTimeOut;
        /// <summary>
        /// Api请求超时时间
        /// </summary>
        public int ApiRequestTimeOut {
            get { return _apiRequestTimeOut; }
            set { _apiRequestTimeOut = value; base.RaisePropertyChanged(); }
        }

        private bool _isEnabledScanCodeCheckRepeatVerify;
        /// <summary>
        /// 是否启用扫码查重验证
        /// </summary>
        public bool IsEnabledScanCodeCheckRepeatVerify {
            get { return _isEnabledScanCodeCheckRepeatVerify; }
            set { _isEnabledScanCodeCheckRepeatVerify = value; }
        }
        #endregion


        public DelegateCommand SaveCommand => new DelegateCommand(() => {
            AppSession.Config.IsEnabledMaterialVerify = this.IsEnabledMaterialVerify;
            AppSession.Config.IsEnabledCylinderCodeVerify = this.IsEnabledCylinderCodeVerify;
            AppSession.Config.ApiRequestTimeOut = this.ApiRequestTimeOut;
            AppSession.Config.IsEnabledScanCodeCheckRepeatVerify = this.IsEnabledScanCodeCheckRepeatVerify;
            AppSession.SaveAppConfig();
            _message.GrowSuccess("保存成功！");
        });

        public DelegateCommand LockCommand => new DelegateCommand(() => {
            if (IsAuth) {
                IsAuth = false;
                LockText = "解锁";
                return;
            }
            _dialog.ShowDialog(nameof(AuthDialogView), new DialogParameters() { { "Title", "用户授权" } }, rhs => {
                IsAuth = (rhs.Result == ButtonResult.OK);
                if (IsAuth) {
                    LockText = "上锁";
                }
            });
        });

        protected override void OnViewLoaded() {
            this.IsAuth = false;
            LockText = "解锁";
            this.IsEnabledMaterialVerify = AppSession.Config.IsEnabledMaterialVerify;
            this.IsEnabledCylinderCodeVerify = AppSession.Config.IsEnabledCylinderCodeVerify;
            this.ApiRequestTimeOut = AppSession.Config.ApiRequestTimeOut;
            this.IsEnabledScanCodeCheckRepeatVerify = AppSession.Config.IsEnabledScanCodeCheckRepeatVerify;
        }

    }
}
